/**
 * KAYO APPLIANCE CHAINS.
 * Copyright (c) 2016-2016 All Rights Reserved.
 */
package com.leku.video.base;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.leku.commons.redis.RedisClientTemplate;
import com.leku.commons.util.Base64Util;
import com.leku.commons.util.CollectionUtil;
import com.leku.commons.util.StringUtil;
import com.leku.video.constants.AppConstant;
import com.leku.video.dto.request.BaseReqDto;
import com.leku.video.dto.result.ResultDto;
import com.leku.video.util.ServletUtil;

/**
 * 功能描述: 服务基类
 *
 * @author suiying
 * @created 2017年9月30日
 * @version 1.0.0
 */
@Controller
public class BaseController {

	@Autowired
	private RedisClientTemplate redisClientTemplate;

	/**
	 * 
	 * 功能描述: 封装基本数据
	 *
	 * @param baseDto
	 * @param request
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	public void preBaseDto(BaseReqDto baseDto, HttpServletRequest request) {
		baseDto.setImei(request.getHeader("dev"));
		baseDto.setChannel(request.getHeader("chn"));
		baseDto.setOs(request.getHeader("os"));
		baseDto.setPkgname(request.getHeader("pkg"));
		baseDto.setVersion(request.getHeader("ver"));
		baseDto.setNwtime(request.getHeader("time"));
		baseDto.setSign(request.getHeader("sign"));
		baseDto.setUserId(request.getHeader("usr"));
		baseDto.setAppCode(request.getHeader("app"));
		baseDto.setIp(ServletUtil.getRealIpAddr(request));

		baseDto.setPs(request.getHeader("ps"));
		baseDto.setDevice(request.getHeader("m"));
		baseDto.setWk(request.getHeader("wk"));
		baseDto.setNetwork(request.getHeader("n"));
		baseDto.setArea(request.getHeader("a"));
	}

	/**
	 * 
	 * 功能描述:获取APP信息
	 *
	 * @param baseDto
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	public String getAppInfo(BaseReqDto baseDto) {

		return "操作系统:" + baseDto.getOs() + ",包名:" + baseDto.getPkgname() + ",版本号:" + baseDto.getVersion();
	}

	/**
	 * 
	 * 功能描述: 校验参数是否有效
	 *
	 * @param baseDto
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	public boolean checkBaseParam(BaseReqDto baseDto) {

		// 校验appcode是否有效
		if (StringUtil.isEmpty(baseDto.getAppCode())
				|| StringUtil.isEmpty(AppConstant.APP_MAP.get(baseDto.getAppCode()))) {
			return false;
		}

		return true;
	}

	/**
	 * 
	 * 功能描述: 数据写入响应(加密)
	 *
	 * @param request
	 * @param response
	 * @param resultDto
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	public void writeReponse(HttpServletRequest request, HttpServletResponse response, ResultDto<?> baseResDto) {

		String debug = request.getParameter("xxx");

		if (isWhiteIp(ServletUtil.getRealIpAddr(request))) {
			debug = AppConstant.DEBUG;
		}

		ResultDto<Object> resultDto = new ResultDto<Object>();
		resultDto.setRet(baseResDto.getRet());
		resultDto.setMsg(baseResDto.getMsg());

		if (AppConstant.DEBUG.equals(debug)) {
			resultDto.setData(baseResDto.getData());
		} else {
			String content = "";
			if (baseResDto.getData() != null) {
				content = JSON.toJSONString(baseResDto.getData());
			}

			if (StringUtil.isEmpty(content)) {
				resultDto.setData("");
			} else {
				resultDto.setData(lekuBase64Encode(content));
			}
		}

		ServletUtil.writeResponse(response, JSON.toJSONString(resultDto));
	}

	/**
	 * 功能描述: base64加密并加扰，加密规则: 第1个字符，第8个字符，第19个字符各添加一个随机字符(从0开始)
	 *
	 * @param data
	 *            加密数据
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@SuppressWarnings("deprecation")
	private String lekuBase64Encode(String data) {
		String val = "";
		try {
			val = Base64Util.encodeBase64(data.getBytes("utf-8"));
		} catch (Exception e) {
		}

		StringBuilder sb = new StringBuilder(100);

		String encodeData = sb.append(StringUtil.getRandomString(1)).append(val.substring(0, 8))
				.append(StringUtil.getRandomString(1)).append(val.substring(8, 19))
				.append(StringUtil.getRandomString(1)).append(val.substring(19, val.length())).toString();

		return encodeData;
	}

	/**
	 * 
	 * 功能描述: 判断是否白名单IP
	 *
	 * @param ip
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private boolean isWhiteIp(String ip) {
		boolean flag = false;

		if (StringUtil.isEmpty(ip)) {
			return flag;
		}

		try {
			Set<String> ips = redisClientTemplate.smembers("RDS:YS_WHITE_IP");
			if (CollectionUtil.isNotEmpty(ips) && ips.contains(ip)) {
				flag = true;
			}
		} catch (Exception e) {
		}

		return flag;
	}
}
